<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="竞赛标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入竞赛标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="比赛地点" prop="pushName">
        <el-input v-model="queryParams.local" placeholder="请输入比赛地点" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
<!--      <el-form-item label="比赛开始时间" prop="startDate">-->
<!--        <el-date-picker clearable v-model="queryParams.startDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择开始时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="比赛结束时间" prop="endDate">-->
<!--        <el-date-picker clearable v-model="queryParams.endDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最大报名人数" prop="personNum">-->
<!--        <el-input-number v-model="queryParams.personNum" placeholder="请输入最大报名人数" clearable></el-input-number>-->
<!--      </el-form-item>-->
      <el-form-item label="分类名称" prop="categoryId">
        <el-select v-model="queryParams.categoryId">
          <el-option v-for="item in categoryList" :key="item.id" :value="item.id" :label="item.categoryName">{{item.categoryName}}</el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="发布时间" prop="pushTime">
        <el-date-picker clearable v-model="queryParams.pushTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['contest:contest:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['contest:contest:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['contest:contest:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['contest:contest:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="竞赛标题" align="center" prop="title" />
      <el-table-column label="竞赛类型" align="center" prop="contestCategory">
        <template slot-scope="scope">
          <span>{{ getCategoryName(scope.row.contestCategory) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column show-overflow-tooltip label="发布者" align="center" prop="pushName" />
      <el-table-column show-overflow-tooltip label="发布时间" align="center" prop="pushTime" />-->
<!--      <el-table-column show-overflow-tooltip label="竞赛描述" align="center" prop="description" />-->
      <el-table-column show-overflow-tooltip label="竞赛描述" align="center" prop="contestInfo" />
      <el-table-column label="允许参加的竞赛人数" align="center" prop="personNum" />
      <!-- <el-table-column label="内容详情" align="center" prop="content" /> -->
<!--      <el-table-column width="200" label="封面" align="center" prop="cover">
        <template slot-scope="scope">
          <img :src="scope.row.cover" style="max-width: 175px; height: auto;" alt="封面">
        </template>
      </el-table-column>-->
      <el-table-column show-overflow-tooltip label="竞赛地址" align="center" prop="local" />
<!--      <el-table-column label="是否允许提交电子档" align="center" prop="isEmail">
        <template slot-scope="scope">
          {{ scope.row.isEmail === '0' ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="是否收费" align="center" prop="isToll">
        <template slot-scope="scope">
          {{ scope.row.isToll === '0' ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="收取费用" align="center" prop="toll" />-->
      <el-table-column label="竞赛开始时间" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="竞赛结束时间" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column show-overflow-tooltip label="主办方" align="center" prop="organizer" />-->
      <!-- <el-table-column show-overflow-tooltip label="发布者id" align="center" prop="pushId" /> -->
      <!-- <el-table-column label="分类id" align="center" prop="categoryId" /> -->
<!--      <el-table-column label="报名开始时间" align="center" prop="enrollStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enrollStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名结束时间" align="center" prop="enrollEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enrollEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已参加人数" align="center" prop="participationNum" />
      <el-table-column label="浏览量" align="center" prop="viewNum" />
      <el-table-column label="是否允许评论" align="center" prop="isComment">
        <template slot-scope="scope">
          {{ scope.row.isComment === '0' ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="isTop">
        <template slot-scope="scope">
          {{ scope.row.isTop === '0' ? '是' : '否' }}
        </template>
      </el-table-column>-->
<!--      <el-table-column label="审核状态" align="center" prop="status">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag v-show="scope.row.status === '0'" type="success">审核通过</el-tag>-->
<!--          <el-tag v-show="scope.row.status === '1'" type="warning">未经审核</el-tag>-->
<!--          <el-tag v-show="scope.row.status === '2'" type="danger">拒绝通过</el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="是否支持团队参加" align="center" prop="isTeam">
        <template slot-scope="scope">
          {{ scope.row.isTeam === '0' ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="团队最大数" align="center" prop="teamNum" />-->
      <el-table-column label="操作" align="center" width="150px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="updateContestInfo(scope.row.id)" v-hasPermi="['contest:contest:edit']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['contest:contest:edit']">修改</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['contest:contest:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import {
	listContest,
  listContestInfo,
	getContest,
	delContest,
	addContest,
	updateContest,
} from "@/api/contest/contest";

import {
  listExpandContestCategory,
} from "@/api/category/category"

import {
  findCategoryChildListByName
} from "@/api/category/category";

export default {
  name: "Contest",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 竞赛表格数据
      contestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        pushTime: null,
        pushName: null,
        description: null,
        personNum: null,
        content: null,
        imgId: null,
        local: null,
        isEmail: null,
        isToll: null,
        toll: null,
        startDate: null,
        endDate: null,
        organizer: null,
        pushId: null,
        categoryId: null,
        enrollStartDate: null,
        enrollEndDate: null,
        participationNum: null,
        viewNum: null,
        isComment: null,
        isTop: null,
        status: null,
        isDel: null,
        isTeam: null,
        teamNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //当前页面分类
      categoryList: []
    };
  },
  created() {
    this.getList();
    this.getCategoryList()
  },

  //由于需要监听vuex中的数据所以还需要计算属性
  computed: {
    Obj() {
      return this.$store.state.monitor.monitorVariables; // 如果monitor模块启用了命名空间
    }
  },


  //监听器
  watch: {
    Obj(newValue) {
      console.log(123);
      if (newValue) {
        console.log(456);
        //如果监控变量变成了true马上刷新页面 获取最新数据
        this.getList()
      }
    }
  },
  methods: {
    /*通过id得到分类的名称*/
    getCategoryName(id) {
      for (let category of this.categoryList) {
        if (category.id == id) {
          return category.categoryName;
        }
      }
      return "未知类型";
    },
    // 发送查询竞赛的接口 因为当前是竞赛页面 所以先查询竞赛然后再查询对应竞赛下所有的子分类最后 但是这个地方直接用一个接口解决了
    async getCategoryList() {
      const {data} = await listExpandContestCategory("竞赛");
      this.categoryList = data
    },
    /*修改或者添加按钮 */
    updateContestInfo(value) {
      this.$router.push({
        path: '/contest/info/add_update',
        query: {
          id: value
        }
      })
    },
    /** 查询竞赛列表 */
    getList() {
      this.loading = true;
      listContestInfo(this.queryParams).then(response => {
        this.contestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        personNum: null,
        content: null,
        imgId: null,
        local: null,
        isEmail: null,
        isToll: null,
        toll: null,
        startDate: null,
        endDate: null,
        organizer: null,
        pushId: null,
        categoryId: null,
        enrollStartDate: null,
        enrollEndDate: null,
        participationNum: null,
        viewNum: null,
        isComment: null,
        isTop: null,
        status: null,
        isDel: null,
        isTeam: null,
        teamNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
	  this.updateContestInfo(null);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let id = row.id || this.ids
      if (this.ids) {
        id = this.ids.pop();
      }
      this.updateContestInfo(id);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除竞赛编号为"' + ids + '"的数据项？').then(function () {
        return delContest(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('contest/contest/export', {
        ...this.queryParams
      }, `contest_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
